package view 
{
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.display.Loader;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.utils.ByteArray;
	import org.bytearray.gif.events.GIFPlayerEvent;
	import org.bytearray.gif.player.GIFPlayer;
	import model.vos.movieType;
	
	/**
	 * 播放器，使用组合模式，可以播放gif动画或者flash动画
	 * @author	Meathill
	 * @version	0.1(2011-04-02)
	 */
	[Event(name = 'complete', type = 'flash.events.Event')]
	public class playerView extends Sprite
	{
		public static const WIDTH:int = 400;
		public static const HEIGHT:int = 300;
		
		public var cur:DisplayObject;
		public var itemWidth:int = 0;
		public var itemHeight:int = 0;
		public var rows:int = 0;
		public var cols:int = 0;
		
		private var _loader:Loader;
		private var _gif:GIFPlayer;
		private var _list:Bitmap;
		private var _is_new_bl:Boolean = false;
		
		public function playerView(p:DisplayObjectContainer = null, posx:int = 0, posy:int = 0) 
		{
			init(p, posx, posy);
		}
		
		/************
		 * properties
		 * *********/
		public function get bmpd():BitmapData {
			if (_list == null || _is_new_bl) {
				makeBmp();
			}
			return _list.bitmapData;
		}
		
		/************
		 * functions
		 * *********/
		private function init(p:DisplayObjectContainer, posx:int, posy:int):void { 
			x = posx, y = posy;
			if (p != null) p.addChild(this);
			
			scrollRect = new Rectangle(0, 0, WIDTH, HEIGHT);
		}
		private function onGifComplete(evt:GIFPlayerEvent):void {
			dispatchEvent(evt);
		}
		
		/************
		 * methods
		 * *********/
		public function addMovie(data:ByteArray, type:int):void {
			if (cur != null) {
				if (cur is GIFPlayer) {
					_gif.dispose();
					_gif.removeEventListener(GIFPlayerEvent.COMPLETE, onGifComplete);
				}
				removeChild(cur);
			}
			switch(type) {
				case movieType.GIF:
					_gif = new GIFPlayer();
					_gif.addEventListener(GIFPlayerEvent.COMPLETE, onGifComplete);
					_gif.loadBytes(data);
					cur = _gif;
					break;
			}
			_is_new_bl = true;
			addChild(cur);
		}
		public function makeBmp(evt:Event = null):void {
			if (_list != null && contains(_list)) {
				removeChild(_list);
			} else if (cur == null) {
				return;
			} else if (_is_new_bl) {
				var _bmpd:BitmapData = new BitmapData(itemWidth * cols, Math.ceil(_gif.totalFrames / cols) * itemHeight, true, 0);
				var _rect:Rectangle = new Rectangle(0, 0, itemWidth, itemHeight);
				var _pos:Point = new Point();
				var _last_bmpd:BitmapData;
				for (var i:int = 0, len:int = _gif.totalFrames; i < len; i += 1) {
					_pos.x = i % cols * itemWidth, _pos.y = int(i / cols) * itemHeight;
					switch(_gif.type) {
						case 1:
							_last_bmpd = _gif.frames[0].bitmapData.clone();
							_last_bmpd.draw(_gif.frames[i].bitmapData);
							break;
						
						case 2:
							_last_bmpd = _gif.frames[i].bitmapData;
					}
					_bmpd.copyPixels(_last_bmpd, _rect, _pos);
				}
				if (_list != null) {
					_list.bitmapData.dispose();
					_list.bitmapData = _bmpd;
				} else {
					_list = new Bitmap(_bmpd);
				}
				_is_new_bl = false;
				addChild(_list);
			} else {
				addChild(_list);
			}
		}
		public function refresh():void {
			_is_new_bl = true;
		}
	}
}